package top.rainbowecho.gateway.security.authentication.social;

import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.social.security.SocialAuthenticationFilter;
import org.springframework.social.security.SpringSocialConfigurer;

/**
 * 用于修改springsocial默认的登录处理路径，并添加social filter到security过滤链
 *
 * @author rainbow
 * @since 2019/12/15 20:25
 */
public class QqSocialConfigurer extends SpringSocialConfigurer {
    private String filterUrl;

    private AuthenticationSuccessHandler authenticationSuccessHandler;

    public void setAuthenticationSuccessHandler(AuthenticationSuccessHandler authenticationSuccessHandler) {
        this.authenticationSuccessHandler = authenticationSuccessHandler;
    }

    public QqSocialConfigurer(String filterUrl) {
        this.filterUrl = filterUrl;
    }

    @Override
    protected <T> T postProcess(T object) {
        SocialAuthenticationFilter filter = (SocialAuthenticationFilter) super.postProcess(object);
        filter.setFilterProcessesUrl(this.filterUrl);
        if (authenticationSuccessHandler != null) {
            // 如果tokenSuccessHandler存在的话，就用此处理器，用QQ的token换一个自己应用的token给前台
            filter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
        }
        return (T) filter;
    }
}
